package com.bkhc.product.repository;

import com.alibaba.fastjson.JSON;
import com.bkhc.common.util.CommonUtil;
import com.bkhc.common.util.response.PageUtil;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.common.vo.OrderItemVo;
import com.bkhc.common.vo.OrderVo;
import com.bkhc.product.entity.Spec;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class OrderRepository {
    @Autowired
    private RestTemplate restTemplate;

    public PageUtil getOrders(OrderVo vo, int pageIndex, int pageSize){
        String serviceName = "ORDER-APP";
        String url = "http://" + serviceName + "/order/list?pageIndex="+pageIndex+"&pageSize="+pageSize;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        PageUtil result = restTemplate.postForObject(url,vo, PageUtil.class);
        return result;
    }

    public List<OrderItemVo> queryOrderItem(String orderCode) {
        String serviceName = "ORDER-APP";
        String url = "http://" + serviceName + "/order/item?orderCode="+orderCode;
        ResultJson<List<OrderItemVo>> result = restTemplate.postForObject(url, null, ResultJson.class);
        return result.getData();
    }

    public PageUtil getProductorOrders(OrderVo order, List<String> productCodes, int pageNum, int pageSize) {
        String serviceName = "ORDER-APP";
        String url = "http://" + serviceName + "/order/order/list/productor?" +
                "pageIndex="+pageNum
                +"&pageSize="+pageSize;
        Map<String,Object> params = new HashMap<>();
        params.put("productCodes",productCodes);
        params.put("userId",order.getUserId());
        params.put("username",order.getUsername());
        params.put("receiver",order.getReceiver());
        params.put("brandCode",order.getBrand());
        params.put("brandName",order.getBrandName());
        params.put("productName",order.getProductName());
        params.put("orderCode",order.getOrderCode());
        params.put("orderStatus",order.getOrderStatus());
        HttpEntity<Object> enetity = CommonUtil.buildParam(params);
        PageUtil result = restTemplate.postForObject(url.toString(), enetity, PageUtil.class);
//        PageUtil result = restTemplate.postForObject(url,productCodes, PageUtil.class);
        return result;
    }

    public void updateOrderStatus(String orderCode,String orderStatus) {
        String serviceName = "ORDER-APP";
        String url = "http://" + serviceName + "/order/order/"+orderCode+"/updateOrderStatus/"+orderStatus;

        HttpEntity<Object> enetity = CommonUtil.buildParam(null);
        PageUtil result = restTemplate.postForObject(url.toString(), enetity, PageUtil.class);
    }

    public void updateOrderAuditStatus(String orderCode,int auditStatus) {
        String serviceName = "ORDER-APP";
        String url = "http://" + serviceName + "/order/order/"+orderCode+"/updateOrderAuditStatus/"+auditStatus;

        HttpEntity<Object> enetity = CommonUtil.buildParam(null);
        PageUtil result = restTemplate.postForObject(url.toString(), enetity, PageUtil.class);
    }

    public PageUtil getOrdersByUserId(OrderVo orderVo, int pageNum, int pageSize) {
        String serviceName = "ORDER-APP";
        String url = "http://" + serviceName + "/order/order/queryOrderByUserId?" +
                "pageIndex="+pageNum
                +"&pageSize="+pageSize;
        Map<String,Object> params = new HashMap<>();
        params.put("userId",orderVo.getUserId());
        params.put("username",orderVo.getUsername());
        params.put("receiver",orderVo.getReceiver());
        params.put("brandCode",orderVo.getBrand());
        params.put("brandName",orderVo.getBrandName());
        params.put("productName",orderVo.getProductName());
        params.put("productCode",orderVo.getProductCode());
        params.put("orderStatus",orderVo.getOrderStatus());
        HttpEntity<Object> enetity = CommonUtil.buildParam(params);
        PageUtil result = restTemplate.postForObject(url.toString(), enetity, PageUtil.class);
        return  result;
    }

    public void assginToPartner(String orderCode) {
        String serviceName = "ORDER-APP";
        String url = "http://" + serviceName + "/order/order/assgin/partner/"+orderCode;
        HttpEntity<Object> enetity = CommonUtil.buildParam(null);
        PageUtil result = restTemplate.postForObject(url.toString(), enetity, PageUtil.class);
    }
}
